#include "ST_onestudentinfo.h"
#include "ui_ST_onestudentinfo.h"

ST_OneStudentInfo::ST_OneStudentInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ST_OneStudentInfo)
{
    ui->setupUi(this);

    TOOLS->setTableHead(ui->tableWidget,{"课程名","课程号","成绩","绩点","学分","通过状态","课程排名"});
}

ST_OneStudentInfo::~ST_OneStudentInfo()
{
    delete ui;
}

void ST_OneStudentInfo::setDataFromID(int id)
{
    auto userInfo = DB->getAllStudentInfo(QString("id = %1").arg(id));
    if(userInfo.isEmpty()){
        qDebug()<<"err";
        return ;
    }

    setInfoToLabel(userInfo.at(0));

    auto grades = DB->getAllStudentGrades(QString("id = %1").arg(id));

    setDataToTable(grades);


}

void ST_OneStudentInfo::setInfoToLabel(const DB_View_StudentInfo &info)
{
    ui->idLabel ->setText(QString::number(info.id));
    ui->nameLabel ->setText(info.name);
    ui->classLabel ->setText(info.className);
    ui->rankLanel->setText(QString::number(info.rank));
}

void ST_OneStudentInfo::setDataToTable(const QList<DB_View_StudentGrades> &data)
{
    ui->tableWidget->setRowCount(data.count());

    for(int r = 0;r<data.count() ; r++){
        auto&& cur = data[r];

        QString info = "%0,%1,%2,%3,%4,%5,%6";
        info = info.arg(cur.coureName)
                .arg(cur.courseID)
                .arg(cur.grade)
                .arg(cur.GPA)
                .arg(cur.credit)
                .arg(cur.grade > 60?"通过":"未通过")
                .arg(cur.rankingInClass);

        QStringList curStrs = info.split(",");

        for(int c = 0 ; c< curStrs.count();c++){
            ui->tableWidget->setItem(r,c,new  QTableWidgetItem(curStrs[c]));
        }
    }
}

void ST_OneStudentInfo::on_pushButton_clicked()
{
    auto path = TOOLS->openCSVSaveDialog(ui->idLabel->text());

    CSV->tabelSaveFile(ui->tableWidget,path);

}

void ST_OneStudentInfo::showEvent(QShowEvent *event)
{
    if(GDATA->userType == 3){
        setDataFromID(GDATA->Account);

        auto res = DB->getAllStudentInfo(QString("id = %1").arg(GDATA->Account));

        TOOLS->showEarlyWarning(res,"您的绩点处于危险边缘，请加强重视，否则可能影响毕业。");

    }
}

